package com.zz.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class sortTest {
    public static void main(String args[]){
        Student[] students = new Student[4];
        students[0] = new Student("林黛玉", 167.7, 20);
        students[1] = new Student("鲁智深", 189, 34);
        students[2] = new Student("孙悟空", 167.8, 23);
        students[3] = new Student("红孩儿", 150, 3);
        //方法一：實現Comparable接口，重寫compareTo方法
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));shixian

        /**
         *  方法二：public static<T> void sort(T[] arr, Comparator<? super T > c)
         *  参数1：需要排序的数组
         *  参数2：Comparable比较器对象（用来制定对象的比较规则）
         */
        Arrays.sort(students, new Comparator <Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                /**
                 * o1 大于 o2 则返回正整数
                 * o1 小于 o2 则返回负整数
                 * o1 等于 o2 则返回0
                 */
                //写法1
//                if(o1.getHeight() > o2.getHeight()){
//                    return 1;
//                }else if(o1.getHeight() < o2.getHeight()){
//                    return -1;
//                }
//                return 0;
                //写法2
                //return Double.compare(o1.getHeight(),o2.getHeight());升序
                return Double.compare(o2.getHeight(),o1.getHeight());//降序
            }
        });
        System.out.println(Arrays.toString(students));
    }
}
